package com.zlh.web.backend;


import com.github.pagehelper.Page;
import com.zlh.model.EbOrder;
import com.zlh.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

   @Autowired
    private OrderService orderService;
   //分页显示
   @RequestMapping("/list")
        public String list(Model model,
                           @RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
       System.out.println("进入OrderController");
       //1.调用service层的分页查询商品信息的方法 （web--service-dao--数据库）
       List<EbOrder> ebOrders = orderService.list(page, pageSize);


       model.addAttribute("ebOrders",ebOrders);//key-value

       model.addAttribute("curPage",page);//2.2 传递最大页数到product.jsp
       model.addAttribute("maxPage",((Page)ebOrders).getPages());

       return "manage/order";

   }

   @RequestMapping("to_edit")
    public String toEit (Model model, Integer eoId){

       EbOrder ebOrder = orderService.selectOne(eoId);

       model.addAttribute("ebOrder",ebOrder);
       return "manage/order-modify";
   }
    @RequestMapping("/update")
    public String updateOrder(EbOrder ebOrder){
        int i = orderService.update(ebOrder);
        System.out.println(ebOrder.getEoId());
        if(i<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //删除订单页面
    @RequestMapping("/delete")
    public String deleteOrder(int eoId){
        int i = orderService.delete(eoId);
        if(i<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //根据订单号或姓名查找指定的信息
    @RequestMapping("/findByOrder")
    public String successOrder(Model model,
                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "pageSize",defaultValue = "5") Integer paseSize,
                               @RequestParam(value = "eoId",defaultValue = "0") int eoId,
                               @RequestParam(value = "eoUserName",defaultValue = "null")String eoUserName){
        System.out.println(eoUserName);
        //查找出ID和姓名的订单信息
        List<EbOrder> ebOrders = orderService.selectSuccess(eoId, eoUserName,page,paseSize);
        System.out.println(ebOrders.toString());
        model.addAttribute("ebOrders",ebOrders);
        model.addAttribute("page",page);
        model.addAttribute("maxPage",((Page)ebOrders).getPages());

        return "manage/order";


    }
}


